//
//  contentPushContller.m
//  ForengnMenu
//
//  Created by jiangtao on 16/1/22.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "contentPushContller.h"
#import "sendView.h"
#import "evaluateCell.h"

#import "evaluateOneCell.h"

@interface contentPushContller ()<UITableViewDelegate,UITableViewDataSource,sendViewDelegete>

@property UIButton * attentionButton;

@property  NSMutableArray * comments;

//发送视图
@property sendView * sendView;

@property NSString * string2;

@property NSString * zanWithString;

@property NSArray * arr;

@property NSMutableArray * number;

@property NSString * zanWithString1;

@property int aValue;
@property (strong, nonatomic) IBOutlet UITableView *mytabelView;

@end

@implementation contentPushContller{
    
    int y;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mytabelView.backgroundColor = [UIColor whiteColor];
    
    //滑动列表时结束编辑（隐藏键盘）
    self.mytabelView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    self.comments =[NSMutableArray new];
    
    self.sendView= [[[NSBundle mainBundle] loadNibNamed:@"sendView" owner:self options:nil] lastObject];
    //指定委托者
    self.sendView.deleget =self;
    
    CGRect rect=self.sendView.frame;
    
    rect.size.width = self.view.frame.size.width;
    rect.origin.y = self.view.frame.size.height - self.sendView.frame.size.height;
    
    self.sendView.frame =rect;
    [self.view addSubview:self.sendView];
    
    //注册一个键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    self.number = [NSMutableArray new];
    
    NSDictionary * dic =self.contextarr.copy;
    
    self.title = dic[@"name"];
    
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
    //拿到键盘的(大小、位置)信息
    CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //拿到发送视图的（位置、大小）信息
    CGRect sendViewRect = self.sendView.frame;
    
    sendViewRect.origin.y = keyboardRect.origin.y - sendViewRect.size.height;
    
    self.sendView.frame = sendViewRect;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section ==0) {
        return 7;
    }else{
    
        return 2;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      NSDictionary * qwe = self.contextarr.copy;
    if (indexPath.section == 0) {
        NSString * indent=@"contentCell";
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        evaluateOneCell * cell= [tableView dequeueReusableCellWithIdentifier:indent];
        
        cell.stepLabel.textAlignment = NSTextAlignmentLeft;
        
        cell.stepLabel.numberOfLines = MAXFLOAT;
    
        if (indexPath.row ==0) {
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens1"]];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens1text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
            
        }else if(indexPath.row == 1){
        
        cell.stepImage.image = [UIImage imageNamed:qwe[@"greens2"]];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens2text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
        
        }else if(indexPath.row ==2){
        
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens3"]];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens3text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
            
        }else if(indexPath.row ==3){
            
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens4"]];
  
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens4text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;

        }else if(indexPath.row ==4){
            
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens5"]];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens5text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
            
        }else if(indexPath.row ==5){
            
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens6"]];
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens6text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
 
        }else if(indexPath.row ==6){
            
            cell.stepImage.image = [UIImage imageNamed:qwe[@"greens7"]];
            
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:qwe[@"greens7text"]];
            
            //设置：在0-3个单位长度内的内容显示成红色
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
            cell.stepLabel.attributedText = str;
        }
    
        return cell;
        
    }else{
        
        NSString * indent1 = @"mycellTwo";
        
        evaluateCell * cell1=[tableView dequeueReusableCellWithIdentifier:indent1];
            cell1.headImage.layer.cornerRadius =20.0f;
            cell1.headImage.layer.masksToBounds = YES;


        if (indexPath.row ==0) {
            
            cell1.nameLabel.text = qwe[@"name1"];
            
            cell1.headImage.image = [UIImage imageNamed:qwe[@"imags1"]];
            
            
            cell1.timeWithLabel.text = qwe[@"htime1"];
            cell1.timeWithLabel.textColor = [UIColor lightGrayColor];
            
            cell1.evaLuteLabel.text = qwe[@"context1"];
        }else{
            
            cell1.nameLabel.text = qwe[@"name2"];
            
            cell1.headImage.image =[UIImage imageNamed:qwe[@"imags2"]];
            
            
            cell1.timeWithLabel.text = qwe[@"htime2"];
            cell1.timeWithLabel.textColor = [UIColor lightGrayColor];

            cell1.evaLuteLabel.text = qwe[@"context2"];

        
        }
        return cell1;

    }

}
- (void)sendMessage:(NSString *)message{
    

    if (message.length==0) {
        return;
    }
    NSString * string = message;
    
    long int index = self.number.count;
    
    //每次添加在第一行
    [self.number insertObject:string atIndex:index];
    //这个单元格位置为  第多少行  第多少节
    NSIndexPath * inde = [NSIndexPath indexPathForRow:index inSection:2];
    //添加到单元格
    [self.mytabelView insertRowsAtIndexPaths:@[inde] withRowAnimation:UITableViewRowAnimationFade];
    
    //定位到某一行
    [self.mytabelView selectRowAtIndexPath:inde animated:YES scrollPosition:UITableViewScrollPositionBottom];
    //隐藏键盘
    [self.view endEditing:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==0) {
        return 180;
    }else{
        return 80;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 440;
        
    }else if(section == 1){
    
        return 50;
    
    }else{
    
        return 10;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section ==0) {
        return 100;
    }else{
    
        return 80;
    
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSDictionary * qwe = self.contextarr.copy;

    if (section == 0) {
        UIView * headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 330, 440)];
        
        
        UIView * myview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 16)];
        
        myview.backgroundColor = [UIColor grayColor];
        
        myview.alpha = 0.1;
        
        [headView addSubview:myview];
        
        headView.backgroundColor = [UIColor whiteColor];
        
        UILabel * headerLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, 20, 320, 40)];
        
        headerLabel.text = qwe[@"name"];
        
        headerLabel.font = [UIFont systemFontOfSize:18];
        
        [headView addSubview:headerLabel];
        
        UILabel * headLabel1=[[UILabel alloc] initWithFrame:CGRectMake(30, 45, 120, 40)];
        
        headLabel1.text = @"创建:2016-06-07";
        
        headLabel1.font = [UIFont systemFontOfSize:10];
        
        headLabel1.textColor = [UIColor grayColor];
        
        [headView addSubview:headLabel1];
    
        UILabel * headLabel2=[[UILabel alloc]initWithFrame:CGRectMake(230, 45, 80, 40) ];
        
        headLabel2.text = @"浏览:123";
        
        headLabel2.font = [UIFont systemFontOfSize:10];
        
        headLabel2.textColor = [UIColor grayColor];
        
        [headView addSubview:headLabel2];
        
        UILabel * headLabel3=[[UILabel alloc] initWithFrame:CGRectMake(280, 45, 80, 40)];
        
        headLabel3.text = @"评价:67";
        
        headLabel3.text = qwe[@"zan"];
        
        headLabel3.font = [UIFont systemFontOfSize:10];
        
        headLabel3.textColor = [UIColor grayColor];
        
        [headView addSubview:headLabel3];
        
        UIView *  lineview = [[UIView alloc] initWithFrame:CGRectMake(30, 80, 290, 1)];
        
        lineview.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
        
        [headView addSubview:lineview];
        
        UIImageView *imagview1 =[[UIImageView alloc] initWithFrame:CGRectMake(30, 85, 40, 40)];
        imagview1.layer.cornerRadius =20.0f;
        imagview1.layer.masksToBounds = YES;
        imagview1.image = [UIImage imageNamed:qwe[@"imges"]];
        
        [headView addSubview:imagview1];
        
        UILabel * headLabel4=[[UILabel alloc] initWithFrame:CGRectMake(80, 80, 80, 40)];
        
        headLabel4.text = qwe[@"position"];;
        
        [headView addSubview:headLabel4];
        
        _attentionButton=[[UIButton alloc] initWithFrame:CGRectMake(240,85, 80, 40)];
        
        NSString * string5  = qwe[@"attention"];
        
        _attentionButton.backgroundColor = [UIColor whiteColor];
        
        [_attentionButton setTitle:string5 forState:UIControlStateNormal];
        
        [_attentionButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        _attentionButton.titleLabel.font = [UIFont systemFontOfSize:10];
        
        [_attentionButton addTarget:self action:@selector(attenAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [headView addSubview:_attentionButton];
        
        UILabel * headLabel6=[[UILabel alloc] initWithFrame:CGRectMake(80, 98, 100, 40)];
        
        headLabel6.font = [UIFont systemFontOfSize:10];
        
        headLabel6.textColor = [UIColor grayColor];
        
        headLabel6.text = qwe[@"autograph"];;
        
        [headView addSubview:headLabel6];
        
        UILabel * headLabel7=[[UILabel alloc] initWithFrame:CGRectMake(10, 130, 300, 100)];
        
        headLabel7.textAlignment = NSTextAlignmentLeft;
        
        headLabel7.numberOfLines = MAXFLOAT;
        
        headLabel7.font = [UIFont systemFontOfSize:12];
        
        headLabel7.textColor = [UIColor grayColor];
        
        headLabel7.text = qwe[@"context"];
        
        [headView addSubview:headLabel7];
    
        UILabel * headLabel8=[[UILabel alloc] initWithFrame:CGRectMake(10, 218, 140, 40)];
        
        headLabel8.font = [UIFont systemFontOfSize:14];
        
        headLabel8.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
        
        headLabel8.text = qwe[@"ntroduce"];
        
        [headView addSubview:headLabel8];
        
        UILabel * headLabel9=[[UILabel alloc] initWithFrame:CGRectMake(10, 248, 300, 80)];
        
        headLabel9.textAlignment = NSTextAlignmentLeft;
        
        headLabel9.numberOfLines = MAXFLOAT;
        
        headLabel9.font = [UIFont systemFontOfSize:12];
        
        headLabel9.textColor = [UIColor grayColor];
        
        headLabel9.text = qwe[@"materia"];
        
        [headView addSubview:headLabel9];
        
        UILabel * headLabel11=[[UILabel alloc] initWithFrame:CGRectMake(10, 290, 100, 100)];
    
        headLabel11.font = [UIFont systemFontOfSize:14];
        
        headLabel11.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
        
        headLabel11.text = qwe[@"ntroduceone"];
        
        [headView addSubview:headLabel11];
        
        
        UILabel * headLabel12=[[UILabel alloc] initWithFrame:CGRectMake(10, 330, 300, 100)];
        
        headLabel12.textAlignment = NSTextAlignmentLeft;
        
        headLabel12.numberOfLines = MAXFLOAT;
        
        headLabel12.font = [UIFont systemFontOfSize:12];
        
        headLabel12.textColor = [UIColor grayColor];
        
        headLabel12.text = qwe[@"shicai"];
        
        [headView addSubview:headLabel12];

        UILabel * zuofa = [[UILabel alloc] initWithFrame:CGRectMake(10, 430, 100, 10)];
        
        zuofa.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
        zuofa.font = [UIFont systemFontOfSize:14];
        zuofa.text = qwe[@"zuofa"];
        
        [headView addSubview:zuofa];
    
        return headView;
    }
    if(section ==1){
        UIView  * cellViewTwo =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];

        UILabel * labelonecell = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 80, 10)];
         labelonecell.text = qwe[@"celltwo"];

        labelonecell.font = [UIFont systemFontOfSize:14];
        
        [cellViewTwo addSubview:labelonecell];
    
        return cellViewTwo;
    
    }
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if (section==0) {
        
        NSDictionary * dis =self.contextarr.copy;
        NSDictionary * f =dis[@"dis"];

        
        UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
        
        UIButton * footButton =[[UIButton alloc] initWithFrame:CGRectMake((footView.frame.size.width/2.0f)-40, 7, 50, 53)];
        
        [footButton setImage:[UIImage imageNamed:@"zanWithOne"] forState:UIControlStateNormal];
        footButton.layer.cornerRadius =25.0f;
        footButton.layer.masksToBounds = YES;
        
        [footView addSubview:footButton];
        
        [footButton addTarget:self action:@selector(BUttonAction) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake((footView.frame.size.width/2.0f)+20, 8, 50, 50)];
   
        
            NSString * g1 =dis[@"zan"];
            NSString * g10 = [g1 substringFromIndex:2];
        
        if (self.zanWithString1 == nil) {
            self.zanWithString = g10;
        }else{
           
            if ([self.zanWithString1 intValue] == [g10 intValue] +1) {
                self.zanWithString = self.zanWithString1;
                label.text = self.zanWithString;
            }
        }
        
        label.text = self.zanWithString;
   
        
        label.font = [UIFont systemFontOfSize:13];
        
        [footView addSubview:label];
        
        UIView  * bannerImage = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 320, 40)];
        
        
        UIImageView * imag1=[[UIImageView alloc] initWithFrame:CGRectMake(50, 5, 30, 30)];
        
        NSString * g =f[@"dis1"];
    
        imag1.image =[UIImage imageNamed:g];
        
        imag1.layer.cornerRadius =15.0f;
        imag1.layer.masksToBounds = YES;
        [bannerImage addSubview:imag1];
        
        
        UIImageView * imag2=[[UIImageView alloc] initWithFrame:CGRectMake(90, 5, 30, 30)];
        
        NSString * g2 =f[@"dis2"];
        imag2.image =[UIImage imageNamed:g2];
        
        imag2.layer.cornerRadius =15.0f;
        imag2.layer.masksToBounds = YES;
        [bannerImage addSubview:imag2];
        
        
        UIImageView * imag3=[[UIImageView alloc] initWithFrame:CGRectMake(130, 5, 30, 30)];
        NSString * g3 =f[@"dis3"];
        imag3.image =[UIImage imageNamed:g3];
        
        imag3.layer.cornerRadius =15.0f;
        imag3.layer.masksToBounds = YES;
        [bannerImage addSubview:imag3];
        
        
        UIImageView * imag5=[[UIImageView alloc] initWithFrame:CGRectMake(170, 5, 30, 30)];
        NSString * g5 =f[@"dis4"];
        imag5.image =[UIImage imageNamed:g5];
        
        imag5.layer.cornerRadius =15.0f;
        imag5.layer.masksToBounds = YES;
        [bannerImage addSubview:imag5];
        
        UIImageView * imag6=[[UIImageView alloc] initWithFrame:CGRectMake(210, 5, 30, 30)];
        
        imag6.image =[UIImage imageNamed:@"gengduo"];
        
        imag6.layer.cornerRadius =15.0f;
        imag6.layer.masksToBounds = YES;
        [bannerImage addSubview:imag6];
        
        NSDictionary * qwe =self.contextarr.copy;
        NSString * imagehead11=qwe[@"imageTwo"];
        
        
        UIImageView *imagview1 =[[UIImageView alloc] initWithFrame:CGRectMake(230, 5, 30, 30)];
        imagview1.layer.cornerRadius =20.0f;
        imagview1.layer.masksToBounds = YES;
        imagview1.image = [UIImage imageNamed:imagehead11];
        
        
        [bannerImage addSubview:imagview1];
        
        bannerImage.backgroundColor = [UIColor colorWithRed:0.705 green:0.713 blue:0.720 alpha:0.6];
        
        
        [footView addSubview:bannerImage];
        
        return footView;
    }else{
        
        return [UIView new];
        
    }
    
    
    
}
-(void)attenAction:(UIButton *)button{
    
    if ([_attentionButton.titleLabel.text isEqualToString:@"关注"]) {
        [_attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
    }else{
        
        [_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    }
    
}

-(void)BUttonAction{
    

    self.aValue = [self.zanWithString intValue];
    
    self.aValue++;
    
    NSString * str = [NSString stringWithFormat:@"%d",self.aValue];
    self.zanWithString1= str;
   
    [self.mytabelView reloadData];
    
}
@end
